
<template>
    <div class="common_page">
        <div class="main_content" ref="mainContent">
            <el-card class="main-card" shadow="never">
                <el-form :model="searchData" ref="queryForm" :inline="true" label-width="68px" style="margin-bottom: -20px">
                    <el-form-item label="分段编号" prop="segmentNumber">
                        <el-input placeholder="请输入内容" v-model="searchData.segmentNumber"></el-input>
                    </el-form-item>
                    <el-form-item label="分段名称" prop="segmentName">
                        <el-input placeholder="请输入内容" v-model="searchData.segmentName"></el-input>
                    </el-form-item>
                    <el-form-item label="批次编号" prop="batchNumber">
                        <el-input placeholder="请输入内容" v-model="searchData.batchNumber"></el-input>
                    </el-form-item>
                    <el-form-item label="厂区" prop="factoryId">
                        <el-select v-model="searchData.factoryId" clearable placeholder="请选择" @change="plantChange">
                            <el-option v-for="item in plantList" :key="item.id" :label="item.plantName" :value="item.id" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="区域" prop="workshopId">
                        <el-select v-model="searchData.workshopId" clearable placeholder="请选择">
                            <el-option v-for="item in workshopList" :key="item.id" :label="item.shopName" :value="item.id" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="状态" prop="state">
                        <el-select v-model="searchData.state" clearable placeholder="请选择">
                            <el-option v-for="item in buildList" :key="item.buildId" :label="item.buildName" :value="item.buildId" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="" prop="templateNumber">
                        <el-button v-hasPermi="['productManage:segmentedManage:query']" type="primary" icon="el-icon-search" size="mini" @click="search">搜索</el-button>
                    </el-form-item>
                </el-form>
                <div class="card-toolbar mb8 flex-between" style="margin-top: 20px; width: 100%">
                    <el-button v-hasPermi="['productManage:segmentedManage:add']" type="primary" icon="el-icon-plus" size="mini" @click="addNew">新增</el-button>
                    <div>
                        <el-tooltip class="item" effect="dark" content="重置" placement="top">
                            <el-button size="mini" circle icon="el-icon-refresh" v-hasPermi="['productManage:segmentedManage:query']" @click="resetFields" />
                        </el-tooltip>
                    </div>
                </div>

                <div class="page_content" style="width: 100%">
                    <div class="page_content_bottom">
                        <sysTable
                            :data="setTableData"
                            :columns="setTableColumns"
                            :curr-page="searchData.currPage"
                            :total-page="pageTotal"
                            :page-size="searchData.pageSize"
                            @sizeChange="sizeChange"
                            @currentChange="currentChange"
                        >
                            <template #state="scope">
                                <div :class="jumpClass(scope.scope.row.state)">{{ jumpState(scope.scope.row.state) }}</div>
                            </template>
                            <template #rateOfProgress="scope">
                                <el-progress :text-inside="true" color="#91c7fd" :stroke-width="28" text-color="#7f7f7f" :percentage="scope.scope.row.rateOfProgress"></el-progress>
                            </template>
                            <template #operation="scope">
                                <el-button v-hasPermi="['productManage:segmentedManage:edit']" size="small" type="primary" plain style="padding: 5px" icon="el-icon-edit" @click="editDialog(scope.scope.row)">
                                    编辑
                                </el-button>
                                <el-button v-hasPermi="['productManage:segmentedManage:detail']" size="small" type="primary" plain style="padding: 5px" icon="el-icon-view" @click="showDetail(scope.scope.row)">
                                    详情
                                </el-button>
                                <el-button v-hasPermi="['productManage:segmentedManage:remove']" size="small" type="danger" plain style="padding: 5px" icon="el-icon-delete" @click="delOne(scope.scope.row)">
                                    删除
                                </el-button>
                            </template>
                        </sysTable>
                    </div>
                </div>
            </el-card>
            <el-dialog :title="addOrEditTitle" :visible.sync="addOrEditDialog" width="748px" append-to-body>
                <el-form ref="form" :model="formData" label-width="100px">
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="关联批次编号:" prop="batchNumber">
                                <el-input v-model="formData.batchNumber" placeholder="请输入内容" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="分段编号:" prop="segmentNumber">
                                <el-input v-model="formData.segmentNumber" placeholder="请输入内容" style="width: 220px" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="分段名称:" prop="segmentName">
                                <el-input v-model="formData.segmentName" placeholder="请输入内容" style="width: 220px" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="所在区域:" prop="workshopId">
                                <el-select v-model="formData.workshopId" clearable placeholder="请选择">
                                    <el-option v-for="item in allWorkShopList" :key="item.count" :label="item.date" :value="item.count" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="备注:" prop="remarks">
                                <el-input v-model="formData.remarks" type="textarea" rows="2" placeholder="请输入备注" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <div v-if="addOrEditTitle == '编辑'" class="dialog_line"></div>
                    <div v-if="addOrEditTitle == '编辑'" class="edit_doalog">
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="生产状态:" prop="state">
                                    <el-radio-group v-model="formData.state">
                                        <el-radio :label="1">待生产</el-radio>
                                        <el-radio :label="2">生产中</el-radio>
                                        <el-radio :label="3">已完成</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="submitForm">提 交</el-button>
                </div>
            </el-dialog>
            <el-dialog title="详情" class="detail_dialog" :visible.sync="detailDialog" width="648px" append-to-body>
                <el-form ref="form" :model="formData" label-width="100px">
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="关联批次编号:" prop="batchNumber">
                                <div class="detail_info">{{ formData.batchNumber }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="分段编号:" prop="segmentNumber">
                                <div class="detail_info">{{ formData.segmentNumber }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="分段名称:" prop="segmentName">
                                <div class="detail_info">{{ formData.segmentName }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="所在区域:" prop="workshopId">
                                <div class="detail_info">{{ formData.workshopName }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="备注:" prop="remarks">
                                <div class="detail_info">{{ formData.remarks }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="状态:" prop="remarks">
                                <div class="special_info" :class="jumpClass(formData.state)">{{ jumpState(formData.state) }}</div>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="cancel">确 认</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import { getSegmentList, editSegmentInfo, delSegmentInfo, addSegmentInfo, getWorkshopList, getFactoryAreaList } from '../api/product/index';
import { getAllWorkShop } from '../../energy/api/energy/energyTotalAnalysis';
import sysTable from '../components/SysTable';
export default {
    name: 'equipmentEnergy',
    components: {
        sysTable,
    },
    data() {
        return {
            searchData: {
                segmentNumber: '',
                segmentName: '',
                batchNumber: '',
                factoryId: '',
                workshopId: '',
                state: '',
                pageSize: 10,
                currPage: 1,
            },
            setTableData: [],
            pageTotal: 0,
            buildList: [
                {
                    buildName: '待生产',
                    buildId: 1,
                },
                {
                    buildName: '生产中',
                    buildId: 2,
                },
                {
                    buildName: '已完成',
                    buildId: 3,
                },
            ],
            plantList: [],
            workshopList: [],
            allWorkShopList: [],
            setTableColumns: [
                {
                    label: '分段编号',
                    prop: 'segmentNumber',
                    minWidth: '180',
                    align: 'center',
                    fixed: 'left',
                    showOverflowTooltip: true,
                },
                {
                    label: '分段名称',
                    prop: 'segmentName',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '批次编号',
                    prop: 'batchNumber',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '预计开始时间',
                    prop: 'beginDate',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '预计结束时间',
                    prop: 'endDate',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '预计用时',
                    prop: 'timeConsuming',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '实际结束',
                    prop: 'factEndDate',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '实际用时',
                    prop: 'timeFact',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '产区',
                    prop: 'factoryName',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '所在区域',
                    prop: 'workshopName',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '状态',
                    prop: 'state',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '操作',
                    prop: 'operation',
                    minWidth: '220',
                    fixed: 'right',
                    align: 'center',
                },
            ],
            buttonLoading: false,
            addOrEditTitle: '新增',
            formData: {
                batchNumber: '',
                segmentNumber: '',
                segmentName: '',
                workshopId: '',
                remarks: '',
            },
            addOrEditDialog: false,
            detailDialog: false,
            productBatchList: [
                {
                    showMore: false,
                },
            ],
        };
    },
    mounted() {
        this.getPageList();
        this.getPlantList();
    },
    methods: {
        search() {
            this.buttonLoading = true;
            this.getPageList();
            this.getPlantList();
        },
        resetFields() {
            this.searchData = {
                segmentNumber: '',
                segmentName: '',
                batchNumber: '',
                factoryId: '',
                workshopId: '',
                state: '',
                pageSize: 10,
                currPage: 1,
            };
            this.plantList = [];
            this.workshopList = [];
            this.search();
        },
        sizeChange(val) {
            this.searchData.pageSize = val;
            this.getPageList();
        },
        currentChange(val) {
            this.searchData.currPage = val;
            this.getPageList();
        },
        getPageList() {
            getSegmentList(this.searchData)
                .then((res) => {
                    this.setTableData = res.data.list;
                    this.pageTotal = res.data.total || 0;
                    this.buttonLoading = false;
                })
                .finally(() => {
                    this.buttonLoading = false;
                });
        },
        getPlantList() {
            getFactoryAreaList().then((res) => {
                if (res.code == 200) {
                    this.plantList = res.data || [];
                } else {
                    this.$message.error('厂区数据请求失败，请联系管理员');
                }
            });
        },
        plantChange(val) {
            this.workshopList = [];
            this.searchData.workshopId = '';
            if (val) {
                this.getWorkshopList(val);
            }
        },
        getWorkshopList(val) {
            getWorkshopList(val).then((res) => {
                if (res.code == 200) {
                    this.workshopList = res.data || [];
                } else {
                    this.$message.error('区域数据请求失败，请联系管理员');
                }
            });
        },
        getAllWorkShop() {
            getAllWorkShop().then((res) => {
                if (res.code == 200) {
                    this.allWorkShopList = res.data || [];
                }
            });
        },
        // handleExport() {
        //   this.buttonLoading = true
        //   var datas = {
        //     number: this.searchData.number,
        //     plantName: this.searchData.plantName,
        //     workshopName: this.searchData.workshopName,
        //     state: this.searchData.state,
        //     pageSize: '',
        //     currPage: ''
        //   }
        //   const obj = JSON.parse(JSON.stringify(datas))
        //   this.download('energy/monitoring/buildingByReport', {
        //             ...obj
        //         }, '订单管理记录.xlsx')
        //   this.buttonLoading = false
        //   // exportBuild(obj).then(res=> {
        //   //   const fileName = '设备能源监控记录.xlsx'
        //   //   saveExcel(res.data, fileName)
        //   // })
        // },
        delOne(val) {
            this.$confirm('此操作将永久删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    delSegmentInfo(val.id).then((res) => {
                        if (res.code == 200) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!',
                            });
                            this.searchData.currPage = 1;
                            this.getPageList();
                        } else {
                            this.$message({
                                type: 'error',
                                message: res.msg,
                            });
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除',
                    });
                });
        },
        addNew() {
            this.addOrEditTitle = '新增';
            this.getAllWorkShop();
            this.addOrEditDialog = true;
        },
        editDialog(val) {
            this.formData = JSON.parse(JSON.stringify(val));
            this.addOrEditTitle = '编辑';
            this.getAllWorkShop();
            this.addOrEditDialog = true;
        },
        showDetail(val) {
            this.formData = val;
            this.detailDialog = true;
        },
        submitForm() {
            if (this.addOrEditTitle == '新增') {
                addSegmentInfo(this.formData).then((res) => {
                    if (res.code == 200) {
                        this.cancel();
                        this.searchData.currPage = 1;
                        this.getPageList();
                    } else {
                        this.$message.error('新增失败，请联系管理员');
                    }
                });
            } else {
                editSegmentInfo(this.formData).then((res) => {
                    if (res.code == 200) {
                        this.cancel();
                        this.searchData.currPage = 1;
                        this.getPageList();
                    } else {
                        this.$message.error('编辑失败，请联系管理员');
                    }
                });
            }
        },
        cancel() {
            this.formData = {
                batchNumber: '',
                segmentNumber: '',
                segmentName: '',
                workshopId: '',
                remarks: '',
            };
            this.detailDialog = false;
            this.addOrEditDialog = false;
        },
        changeStatus(item) {
            item.showMore = !item.showMore;
        },
        jumpState(val) {
            switch (val) {
                case 1:
                    return '待生产';
                case 2:
                    return '生产中';
                case 3:
                    return '已完成';
                default:
                    return '-';
            }
        },
        jumpClass(val) {
            switch (val) {
                case 1:
                    return 'waits';
                case 2:
                    return 'processs';
                case 3:
                    return 'completes';
                default:
                    return '-';
            }
        },
    },
};
</script>

<style lang="scss" scoped>
.common_page {
    height: 100%;
    width: 100%;
    background: #f3f4f5;
    overflow: auto;
}
.main_content {
    padding: 16px 20px;
}
::v-deep.el-card {
    border-radius: 8px;
}
.page_content {
    background: #fff;
    padding-bottom: 30px;
    box-sizing: border-box;
    padding: 20px 20px 30px 20px;
    border-radius: 8px;
    .page_content_head {
        display: flex;
    }
}
.main_content_top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    .main_content_top_box {
        width: 13%;
        margin-right: 1.5%;
        height: 104px;
        background: #fff;
        box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.02), 0px 10px 20px 0px rgba(0, 0, 0, 0.05);
        border-radius: 8px 8px 8px 8px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        .top_box_head {
            color: #666;
            font-size: 14px;
            line-height: 20px;
            margin-bottom: 4px;
        }
        .top_box_content {
            color: #333;
            font-size: 14px;
            // line-height: 30px;
            display: flex;
            align-items: flex-end;
            justify-content: center;
            .top_box_content_num {
                font-size: 26px;
                font-weight: bold;
                line-height: 30px;
            }
            .top_box_content_unit {
                line-height: 22px;
            }
        }
        .top_box_red {
            color: #ff4c4d;
        }
        .top_box_green {
            color: #00b24a;
        }
    }
    .main_content_top_box:last-of-type {
        margin-right: 0px;
    }
}
::v-deep.sys_table {
    .tableHeaderRow {
        cursor: pointer;
        .tableHeaderCell:active {
            opacity: 0.6;
        }
        .tableHeaderCell:last-of-type:active {
            opacity: 1;
        }
        .tableHeaderCell:first-of-type:active {
            opacity: 1;
        }
        .tableHeaderCell:last-of-type {
            cursor: default;
        }
        .tableHeaderCell:first-of-type {
            cursor: default;
        }
    }
}
.ope_btn {
    width: 44px;
    height: 28px;
    line-height: 28px;
    color: #3399ff;
    font-size: 14px;
    background: #ffffff;
    border-radius: 4px 4px 4px 4px;
    border: 1px solid rgba(0, 85, 255, 0.5);
    padding: 0px;
}
.del_btn {
    width: 44px;
    height: 28px;
    background: #ffffff;
    line-height: 28px;
    color: #ff4c4d;
    font-size: 14px;
    border-radius: 4px 4px 4px 4px;
    opacity: 1;
    border: 1px solid rgba(255, 76, 77, 0.5);
    padding: 0px;
}
::v-deep.el-form {
    .el-form-item {
        .el-form-item__label {
            // text-align: left;
            color: #666;
            font-size: 14px;
            font-weight: 400;
        }
    }
}
::v-deep .el-dialog {
    .el-dialog__body {
        padding: 24px;
    }
}
.add_dialog_top {
    border: 1px solid rgba(0, 0, 0, 0.08);
    box-sizing: border-box;
    padding: 16px;
}
.add_dialog_bottomTitle {
    color: #4a4a4a;
    font-size: 16px;
    line-height: 24px;
    margin: 20px 0;
    font-weight: 500;
}
.add_dialog_addBtn {
    height: 52px;
    background: rgba(51, 153, 255, 0.1);
    border-radius: 0px 0px 0px 0px;
    opacity: 1;
    border: 1px solid rgba(0, 0, 0, 0.08);
    display: flex;
    align-items: center;
    font-size: 14px;
    line-height: 20px;
}
.add_dialog_bottom {
    box-sizing: border-box;
    padding: 16px;
    border: 1px solid rgba(0, 0, 0, 0.08);
    background: #f7f8f9;
    margin-bottom: 10px;
    height: 52px;
    box-sizing: border-box;
    overflow: hidden;
    .add_dialog_bottom_top {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 10px;
    }
}
.add_dialog_bottom_top_expansion {
    height: unset;
    box-sizing: border-box;
    overflow: visible;
    .add_dialog_bottom_top {
        border-bottom: 1px solid rgba(0, 0, 0, 0.08);
        padding-bottom: 12px;
    }
}
.add_dialog_bottom_bottom {
    ::v-deep.el-form {
        .el-form-item {
            margin-bottom: 10px;
        }
        .el-form-item:nth-of-type(4) {
            margin-bottom: 0px;
        }
        .el-form-item:nth-of-type(5) {
            margin-bottom: 0px;
        }
        .el-form-item:nth-of-type(6) {
            margin-bottom: 0px;
        }
    }
}

.processs {
    width: 58px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    background: rgba(255, 119, 51, 0.1);
    border-radius: 4px 4px 4px 4px;
    color: #ff7733;
    font-size: 14px;
    margin: 0 auto;
}

.waits {
    width: 58px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    background: rgba(51, 153, 255, 0.1);
    border-radius: 4px 4px 4px 4px;
    color: #3399ff;
    font-size: 14px;
    margin: 0 auto;
}

.completes {
    width: 58px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    background: rgba(0, 178, 74, 0.1);
    border-radius: 4px 4px 4px 4px;
    color: #00b24a;
    font-size: 14px;
    margin: 0 auto;
}

.detail_info {
    font-size: 14px;
    line-height: 20px;
    color: #333;
}

.special_info {
    height: 20px;
    line-height: 20px;
    margin: 0px 0px;
    // margin-top: 4px;
}

.dialog_line {
    height: 1px;
    width: 100%;
    background: rgba(0, 0, 0, 0.08);
    margin-bottom: 20px;
}

.edit_doalog {
    ::v-deep.el-form-item {
        // margin-bottom: 20px;
        .el-form-item__label {
            line-height: 36px;
            text-align: left;
            color: #666;
            font-size: 14px;
            font-weight: 400;
        }
    }
}
.detail_dialog {
    ::v-deep.el-form-item {
        // margin-bottom: 20px;
        .el-form-item__label {
            line-height: 20px;
            text-align: left;
            color: #666;
            font-size: 14px;
            font-weight: 400;
        }
    }
}
</style>
